package com.woniu.service.impl;

import com.woniu.entity.Dept1;
import com.woniu.entity.Emprole1;
import com.woniu.mapper.DeptDao;
import com.woniu.mapper.EmpDao;
import com.woniu.mapper.RoleDao;
import com.woniu.pojo.*;
import com.woniu.service.DeptService;
import com.woniu.service.EmpRoleService;
import com.woniu.service.EmpService;
import com.woniu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DeptServiceImpl implements DeptService {

    @Autowired
    DeptDao deptDao;
    @Autowired
    EmpDao empDao;
    @Autowired
    RoleDao roleDao;
    @Autowired
    RoleService roleService;
    @Autowired
    EmpRoleService empRoleService;

    /**
     * 增加一个院系
     * */
    @Override
    public void add(Dept dept) {
        deptDao.insert(dept);
    }

    /**
     * 根据ID删除一个院系
     * */
    @Override
    public void deleteById(Integer id) {

        deptDao.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除院系
     * */
    @Override
    public void deleteByIds(List<Integer> deptIds) {
        DeptExample deptExample=new DeptExample();
        deptExample.createCriteria().andDeptIdIn(deptIds);
        int i = deptDao.deleteByExample(deptExample);
    }

    /**
     * 根据ID查找一个院系
     * */
    @Override
    public Dept1 findOne(Integer id) {
        Dept1 dept = deptDao.selectByPrimaryKey1(id);
        return dept;
    }

    /**
     * 根据ID修改一个院系
     * */
    @Override
    public void updateById(Dept dept) {
        deptDao.updateByPrimaryKeySelective(dept);
    }

    /**
     * 根据条件查找院系
     * */
    @Override
    public List<Dept1> findByExample(Dept dept) {
        DeptExample example = new DeptExample();
        DeptExample.Criteria criteria = example.createCriteria();
        if (dept.getDeptName()!=null) {
            criteria.andDeptNameEqualTo(dept.getDeptName());
        }
        if (dept.getEmpId()!=null) {
            criteria.andEmpIdEqualTo(dept.getEmpId());
        }
        List<Dept1> depts = deptDao.selectByExample1(example);
        return depts;
    }

    @Override
    public List<Emp> empnameSelect() {
        return null;
    }

    /**
     * 查询院系主管级别的员工
     * */
    @Override
    public List<Emp> findZg(){
        //查到主管级别的角色ID
        List<Emp> empList =new ArrayList<>();
        List<Role> list = roleService.findAll("主管");
        List<Emprole1> byRoleId = empRoleService.findByRoleId(list.get(0).getRoleId());

        for (Emprole1 e : byRoleId) {
            empList.add(e.getEmpId());
        }
        return empList;
    }

    @Override
    public List<Dept> selectAll() {
        return deptDao.selectByExample(null);
    }
}
